import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import tool.TreeNode;
import tool.TreeNodePrint;
import tool.input;

/**
 * @Description: 二叉树的右视图
 * 给定一个二叉树的 根节点 root，想象自己站在它的右侧，
 * 按照从顶部到底部的顺序，返回从右侧所能看到的节点值。
 * @Date:Create in 21:13  2022/7/19
 */
public class rightSideView0199 {

    public static void main(String[] args) {
        String s = "[1,2,3,null,5,null,4]";
        TreeNode root = input.getTree(s);
        TreeNodePrint.printTree(root);
        List<Integer> res = rightSideView1(root);
        System.out.println(res.toString());
    }

    //bfs
    public static List<Integer> rightSideView1(TreeNode root) {
        List<Integer> res = new ArrayList<Integer>();
        if (root == null) return res;
        Queue<TreeNode> que = new LinkedList<>();
        que.offer(root);

        while (!que.isEmpty()) {
            int len = que.size();
            while (len > 0) {
                TreeNode node = que.poll();
                if (node.left != null) que.offer(node.left);
                if (node.right != null) que.offer(node.right);
                len--;
                if(len==0) res.add(node.val);
            }
        }
        return res;
    }

    //dfs
    List<Integer> res = new ArrayList<Integer>();
    public List<Integer> rightSideView2(TreeNode root) {

        if (root == null) return res;
        dfs(root,0);
        return res;
    }
    public void dfs(TreeNode root,int depth) {
        if(root==null) return;
        if(depth==res.size()) res.add(root.val);
        depth++;
        dfs(root.right,depth);
        dfs(root.left,depth);
    }
}
